#include "stdafx.h"
#include "HBGlobalTools.h"





QString CHBGlobalTool::Int2Str( qint64 iVal )
{
	QString str;
	str.setNum(iVal);
	return str;
}

QString CHBGlobalTool::Int2Str( int iVal )
{
	QString str;
	str.setNum(iVal);
	return str;
}

QString CHBGlobalTool::TrimStr( QString &sSrcStr, const QString& sTargets )
{
	sSrcStr = TrimStrLeft(sSrcStr, sTargets);
	return TrimStrRight(sSrcStr, sTargets);
}

QString CHBGlobalTool::TrimStrLeft( QString &sSrcStr, const QString& sTargets )
{
	if( sTargets.isEmpty() || sSrcStr.isEmpty())
	{
		return sSrcStr;
	}

	int iCurBeginPos = 0, iTargetsPos = 0;
	while(true)
	{
		iTargetsPos = sSrcStr.indexOf(sTargets, iCurBeginPos);
		if (iTargetsPos == iCurBeginPos)
		{
			iCurBeginPos += sTargets.size();
		}
		else
		{
			break;
		}
	}

	sSrcStr = sSrcStr.mid(iCurBeginPos, sSrcStr.size() - iCurBeginPos);
	return sSrcStr;
}

QString CHBGlobalTool::TrimStrRight( QString &sSrcStr, const QString& sTargets )
{
	if( sTargets.isEmpty() || sSrcStr.isEmpty())
	{
		return sSrcStr;
	}

	int iTargetsSize = sTargets.size();
	int iCurBeginPos = sSrcStr.size()-1, iTargetsPos = 0;
	while(true)
	{
		iTargetsPos = sSrcStr.lastIndexOf(sTargets, iCurBeginPos);
		if (iTargetsPos + iTargetsSize - 1 == iCurBeginPos)
		{
			iCurBeginPos -= sTargets.size();
		}
		else
		{
			break;
		}
	}

	sSrcStr = sSrcStr.mid(0, iCurBeginPos+1);
	return sSrcStr;
}

vecStr CHBGlobalTool::SplitStr( const QString &str, const QString &sepStr )
{
	vecStr vecSplitStr;
	int nSep =- 1;
	QString tmp, strTemp = str;
	TrimStrLeft(strTemp, sepStr);
	TrimStrRight(strTemp, sepStr);
	while( !strTemp.isEmpty() && ( nSep = strTemp.indexOf( sepStr ) ) >  0 )
	{
		tmp = strTemp.mid( 0, nSep );
		strTemp = strTemp.mid( nSep, strTemp.size() - nSep );

		TrimStrLeft(strTemp, sepStr);
		TrimStrRight(strTemp, sepStr);
		TrimStrLeft(tmp, sepStr);
		TrimStrRight(tmp, sepStr);

		if( !tmp.isEmpty() )
		{
			vecSplitStr.push_back( tmp );
		}
	}

	TrimStrLeft(strTemp, sepStr);
	TrimStrRight(strTemp, sepStr);

	if( !strTemp.isEmpty() )
	{
		vecSplitStr.push_back( strTemp );
	}

	return vecSplitStr;
}